<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;
use app\common\model\User as UserModel;
use think\Db;
use think\Config;

class Login extends HomeBase
{
	protected $user_model;
	protected function _initialize(){
		parent::_initialize();
		$this->user_model = new UserModel();
	}

	/**
     * 用户登录
     * @param string phone
     * @param int    password
     */
	public function index()
	{
		//接收表单传过来的参数
		$phone = $_POST['phone'];
		$password = $_POST['password'];
		//从数据库中查询数据
		$userList = Db('user')->where('phone', $phone)->select();
		//判断数据库中是否有这个电话号码
		if (!count($userList)) {
			$this->apiError('该电话尚未注册,请注册后登录');
		}else{
			$pwd = $userList[0]['password'];
			$pwd = md5($pwd . Config::get('salt'));
			//判断密码是否正确
			if($pwd == $password){
				unset($userList[0]['password']);
				unset($userList[0]['status']);
				$this->apiSuccess($userList);
			}else{
				$this->apiError('密码错误,请重新输入密码');
			}
		}

		
	}



}